package com.atguigu.mr.logger;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @description: xxx
 * @time: 20200102 15:26
 * @author: baojinlong
 **/
public class LogEtlMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取一行
        String line = value.toString();
        boolean okLogIs = parseLog(line, context);
        if (!okLogIs) {
            return;
        }
        context.write(value, NullWritable.get());
    }

    private boolean parseLog(String line, Context context) {
        String[] fields = line.split(" ");
        if (fields.length > 11) {
            // 根据etl处理逻辑进行处理
            context.getCounter("mapStage", "true").increment(1);
            return true;
        } else {
            context.getCounter("mapStage", "false").increment(1);
            return false;
        }
    }
}
